package com.smart.village.big.screen.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.smart.village.big.screen.domain.PartyMemberLogStaticVo;
import com.smart.village.big.screen.domain.PartyOrganizationApplicationStaticVo;
import com.smart.village.big.screen.mapper.PartyOrganizationApplicationMapper;
import com.smart.village.big.screen.service.PartyOrganizationApplicationService;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class PartyOrganizationApplicationServiceImpl implements PartyOrganizationApplicationService {

    @Autowired
    private PartyOrganizationApplicationMapper partyOrganizationApplicationMapper;

    @Override
    public List<PartyOrganizationApplicationStaticVo> getPartyOrganizationApplication(String partyOrganizationSerialNumber,String rowStatus) {
        return partyOrganizationApplicationMapper.getPartyOrganizationApplication(partyOrganizationSerialNumber,rowStatus);
    }

    @Override
    public JSONObject getPartyOrganizationApplicationStatic(String partyOrganizationSerialNumber) {
        List<Map> partyOrganizationApplicationStatic = partyOrganizationApplicationMapper.getPartyOrganizationApplicationStatic(partyOrganizationSerialNumber);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type1",0);
        jsonObject.put("type2",0);
        jsonObject.put("type3",0);
        jsonObject.put("type4",0);
        jsonObject.put("type5",0);
        partyOrganizationApplicationStatic.forEach(map -> {
            if(map.get("applicationStatus").toString().equals("入党申请人")){
                jsonObject.put("type1",map.get("cnt"));
            }
            if(map.get("applicationStatus").toString().equals("入党积极分子")){
                jsonObject.put("type2",map.get("cnt"));
            }
            if(map.get("applicationStatus").toString().equals("发展对象")){
                jsonObject.put("type3",map.get("cnt"));
            }
            if(map.get("applicationStatus").toString().equals("中共预备党员")){
                jsonObject.put("type4",map.get("cnt"));
            }
            if(map.get("applicationStatus").toString().equals("正式党员")){
                jsonObject.put("type5",map.get("cnt"));
            }
        });
        return jsonObject;
    }


    @Override
    public List<PartyMemberLogStaticVo> getPartyMemberLogStaticDetail(String applicationSerialNumber) {
        List<PartyMemberLogStaticVo> partyMemberLogStaticDetail = partyOrganizationApplicationMapper.getPartyMemberLogStaticDetail(applicationSerialNumber);
        boolean will = false;
        for (PartyMemberLogStaticVo partyMemberLogStatic : partyMemberLogStaticDetail) {
            if (partyMemberLogStatic.getOperationDatetime() != null) {
                partyMemberLogStatic.setOperationStatus("已完成");
            }
            if (partyMemberLogStatic.getOperationDatetime() == null) {
                partyMemberLogStatic.setOperationStatus("未完成");
                if(!will){
                    partyMemberLogStatic.setOperationStatus("待完成");
                    will = true;
                }
            }

        }
        return partyMemberLogStaticDetail;
    }

    @Override
    public List<PartyMemberLogStaticVo> getMicroPartyMemberLogStaticDetail(String iDCardNumber) {
        List<PartyMemberLogStaticVo> partyMemberLogStaticDetail = Lists.newArrayList();
        String maxApplicationSerialNumber = partyOrganizationApplicationMapper.getMaxApplicationSerialNumberByIDCardNumber(iDCardNumber);
//        if(StringUtils.isNotEmpty(maxApplicationSerialNumber)) {
            partyMemberLogStaticDetail = partyOrganizationApplicationMapper.getMicroPartyMemberLogStaticDetail(maxApplicationSerialNumber);
            boolean will = false;
            for (PartyMemberLogStaticVo partyMemberLogStatic : partyMemberLogStaticDetail) {
                partyMemberLogStatic.setApplicationSerialNumber(maxApplicationSerialNumber);
                if (partyMemberLogStatic.getOperationDatetime() != null) {
                    partyMemberLogStatic.setOperationStatus("已完成");
                    continue;
                }
                if (partyMemberLogStatic.getOperationDatetime() == null) {
                    partyMemberLogStatic.setOperationStatus("未完成");
                    if (!will) {
                        partyMemberLogStatic.setOperationStatus("待完成");
                        will = true;
                        continue;
                    }
                    continue;
                }

            }
//        }
        return partyMemberLogStaticDetail;
    }

    @Override
    public List<PartyMemberLogStaticVo> getMicroManagerPartyMemberLogStaticDetail(String applicationSerialNumber) {
        List<PartyMemberLogStaticVo> partyMemberLogStaticDetail = Lists.newArrayList();
        if(StringUtils.isNotEmpty(applicationSerialNumber)) {
            partyMemberLogStaticDetail = partyOrganizationApplicationMapper.getMicroPartyMemberLogStaticDetail(applicationSerialNumber);
            boolean will = false;
            for (PartyMemberLogStaticVo partyMemberLogStatic : partyMemberLogStaticDetail) {
                partyMemberLogStatic.setApplicationSerialNumber(applicationSerialNumber);
                if (partyMemberLogStatic.getOperationDatetime() != null) {
                    partyMemberLogStatic.setOperationStatus("已完成");
                    continue;
                }
                if (partyMemberLogStatic.getOperationDatetime() == null) {
                    partyMemberLogStatic.setOperationStatus("未完成");
                    if (!will) {
                        partyMemberLogStatic.setOperationStatus("待完成");
                        will = true;
                        continue;
                    }
                    continue;
                }

            }
        }
        return partyMemberLogStaticDetail;
    }

    @Override
    public String getIfExistPartyOrganizationApplication(String iDCardNumber) {
        return partyOrganizationApplicationMapper.getMaxApplicationSerialNumberByIDCardNumber(iDCardNumber);
    }
}
